package dice.sinanya.tools.getinfo;

import dice.sinanya.db.attr.InputCustomAttr;
import dice.sinanya.db.attr.SelectAttr;
import dice.sinanya.entity.EntityRoleTag;

import java.util.HashMap;

import static dice.sinanya.db.roles.SelectRoles.selectRoleInfoCacheFromDatabase;
import static dice.sinanya.system.MessagesAttr.*;

/**
 * @author SitaNya
 * @date 2020/5/27
 * @email sitanya@qq.com
 * @qqGroup 162279609
 * 有任何问题欢迎咨询
 * <p>
 * 类说明:
 */
public class AttrList {
    static InputCustomAttr inputCustomAttr = new InputCustomAttr();
    static SelectAttr selectAttr = new SelectAttr();

    public static String getAttr(String groupId, long qqId, String role, String attrName) {
        return getAttr(Long.parseLong(groupId), qqId, role, attrName);
    }

    public static void setAttr(String groupId, String attrName, String function) {
        setAttr(Long.parseLong(groupId), attrName, function);
    }

    public static void deleteAttr(String groupId) {
        deleteAttr(Long.parseLong(groupId));
    }

    public static String getAttr(long groupId, long qqId, String role, String attrName) {
        if (!customAttrList.getOrDefault(groupId, new HashMap<>()).containsKey(attrName)) {
            selectAttr.flushAttrList();
        }
        String groupFunction = customAttrList.getOrDefault(groupId, new HashMap<>()).getOrDefault(attrName, attrName);
        if (!groupFunction.equals(attrName)) {
            return groupFunction;
        }
        EntityRoleTag roleTag = new EntityRoleTag(qqId, role);
        if (!pcAttrList.getOrDefault(roleTag, new HashMap<>()).containsKey(attrName)) {
            selectRoleInfoCacheFromDatabase(String.valueOf(qqId), role);
        }
        String pcFunction = pcAttrList.getOrDefault(roleTag, new HashMap<>()).getOrDefault(attrName, attrName);
        if (!pcFunction.equals(attrName)) {
            return pcFunction;
        }

        return attrList.getOrDefault(attrName, attrName);
    }

    public static void setAttr(long groupId, String attrName, String function) {
        inputCustomAttr.insertAttr(groupId, attrName, function);
    }

    public static void deleteAttr(long groupId) {
        inputCustomAttr.removeAttr(groupId);
    }
}
